package com.feinno.module.security.taglib;

import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.shiro.SecurityUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.feinno.module.security.domain.Menu;
import com.feinno.module.security.domain.Resource;
import com.feinno.module.security.domain.User;
import com.feinno.module.security.service.MenuService;

public class MenuTag extends TagSupport {
	/**
	 * 版本号
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 展开方式
	 */
	private String typeFunction;

	/**
	 * 结束标签
	 */
	public int doEndTag() throws JspException {
		// TODO Auto-generated method stub
		return SKIP_BODY;
	}

	/**
	 * 开始标签
	 */
	public int doStartTag() throws JspException {
		JspWriter out = this.pageContext.getOut();
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(this.pageContext.getServletContext());
		MenuService menuManager = (MenuService) wac.getBean("menuService");
		Map<Long, Menu> menuMap = new LinkedHashMap<Long, Menu>();
		List<Object> resourcelist = new LinkedList<Object>();
		User user = (User) SecurityUtils.getSubject().getPrincipal();
		if (user != null) {
			try {
				List<Object> list = menuManager.queryAuthorisedMenus(user.getId());
				Iterator<Object> ie = list.iterator();
				while (ie.hasNext()) {
					Object[] obj = (Object[]) ie.next();
					Resource resource = (Resource) obj[0];
					resourcelist.add(resource);
					Menu menu = (Menu) obj[1];
					menuMap.put(menu.getId(), menu);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			try {
				String s = "";
				for (Iterator<Long> it = menuMap.keySet().iterator(); it.hasNext();) {
					Menu menu = (Menu) menuMap.get(it.next());
					s += "<h1 class='title'><a href='javascript:void(0)' ><img src='" + menu.getIcon() + "' align='absmiddle'>" + menu.getName() + "</a></h1>"
							+ "\n";
					s += "<div class='list'>" + "\n";
					s += "<dl>" + "\n";
					for (int m = 0; m < resourcelist.size(); m++) {
						Resource userre = (Resource) resourcelist.get(m);
						if (userre.getMenu().getName().equals(menu.getName()) && userre.getResType().equalsIgnoreCase("url")) {
							s += "<li><a href='" + userre.getResString() + "' target='mainFrame'>" + userre.getName() + "</a></li>" + "\n";
						}
					}
					s += "</dl>" + "\n";
					s += "</div>" + "\n";
				}
				out.print(s + "\n");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return SKIP_BODY;
	}

	public String getTypeFunction() {
		return typeFunction;
	}

	public void setTypeFunction(String typeFunction) {
		this.typeFunction = typeFunction;
	}
}
